<template>
	<a-row :gutter="16" type="flex">
		<a-col :order="isMobile ? 2 : 1" :md="24" :lg="16">
			<a-form :form="form" ref="ruleForm" autocomplete="off" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<a-form-item label="旧密码">
					<a-input-password
						autocomplete="new-password"
						placeholder="请输入旧密码"
						style="width: 100%;"
						v-decorator="['oldPassword', { rules: [{ required: true,message: '长度为6~30位，必须包含数字、字母、符号中至少一种' }, { validator: checkConfirmold }],getValueFromEvent: removeSpaces }]"
					></a-input-password>
				</a-form-item>
				<a-form-item label="新密码">
					<a-input-password
						autocomplete="new-password"
						placeholder="请输入新密码"
						style="width: 100%;"
						v-decorator="['newPassword', { rules: [{ required: true, message: '长度为6~30位，必须包含数字、字母、符号中至少一种' }, { validator: validateToNextPassword }],getValueFromEvent: removeSpaces }]"
					></a-input-password>
				</a-form-item>
				<a-form-item label="确认密码">
					<a-input-password
						autocomplete="new-password"
						placeholder="请再次输入新密码"
						v-decorator="['confirm_password', { rules: [{ required: true, message: '长度为6~30位，必须包含数字、字母、符号中至少一种' }, { validator: compareToFirstPassword }],getValueFromEvent: removeSpaces }]"
						@blur="handleConfirmBlur"
					></a-input-password>
				</a-form-item>
				<a-form-item :wrapper-col="{ span: 14, offset: 5 }">
					<a-button type="primary" @click="changePwd" :loading="confirmLoading">确定</a-button>
					<a-button class="ant-btn-yellow ml10" @click="cancel">关闭</a-button>
				</a-form-item>
			</a-form>
		</a-col>
	</a-row>
</template>

<script>
import { sysManageapi } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
import { isPwd} from '@/utils/validate';
import { mapActions, mapState } from 'vuex';
import QS from 'qs';
import md5 from 'md5';
import eventBus from '@/utils/eventBus';
export default {
	data() {
		return {
			confirmDirty: false,
			isHasMdspwd:0,
		};
	},
	computed: {
		...mapState(['isMobile'])
	},
	mixins: [ModalMixins],
	props: {
		username: {
			type: String,
			default: ''
		}
	},
	created(){
		this.getMd5PassIsNull();
	},
	methods: {
		/*校验旧密码*/
		checkConfirmold(rule, value, callback) {
			if (value) {
				if (!isPwd(value)) {
					// 正则验证
					callback('长度为6~30位，必须包含数字、字母、符号中至少一种'); // 校验未通过
				}
			}
			callback(); // 校验通过
		},

		handleConfirmBlur(e) {
			const value = e.target.value;
			this.confirmDirty = this.confirmDirty || !!value;
		},
		compareToFirstPassword(rule, value, callback) {
			const form = this.form;
			if (value) {
				if (!isPwd(value)) {
					// 正则验证
					callback('长度为6~30位，必须包含数字、字母、符号中至少一种'); // 校验未通过
				} else if (value !== form.getFieldValue('newPassword')) {
					callback('两次输入的密码不匹配');
				} else {
					callback();
				}
			} else {
				callback();
			}
		},
		validateToNextPassword(rule, value, callback) {
			const form = this.form;
			if (value) {
				if (!isPwd(value)) {
					// 正则验证
					callback('密码由6~30位的数字、字母和符号组成'); // 校验未通过
				} else if (this.confirmDirty) {
					form.validateFields(['confirm_password'], {
						force: true
					});
				}
			}
			callback();
		},
		resetForm() {
			this.form.resetFields();
		},
		
		//获取是否有md5密码
		getMd5PassIsNull() {
			this.$postAction(this.$api.getMd5PassIsNull+'?username='+this.username.trim())
				.then(res => {
					if (res && res.code == 1) {
						this.isHasMdspwd=res.data;
						console.log("this.isHasMdspwd"+this.isHasMdspwd)
						
					}
				})
				.catch(err => {
					//返回错误
				});
		},
		changePwd() {
			const _this = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
				// 验证表单没错误
				if (!err) {
					let parameter = { username: this.username, newPassword: values.newPassword, oldPassword: values.oldPassword };
					if(_this.isHasMdspwd==1){
						parameter.oldPassword=md5(values.oldPassword)
					}
					parameter.newPassword=md5(values.newPassword),
					_this.confirmLoading = true;
					_this.$postAction(sysManageapi.userUrl.changePwd, parameter)
						.then(res => {
							if (res && res.code == 1) {
								_this.$message.success('修改成功');
								this.form.resetFields();
							}
						})
						.finally(() => {
							_this.confirmLoading = false;
						});
				}
			});
		},
		/* 点击取消关闭页面 */
		cancel() {
			eventBus.$emit('closePage');
		}
	}
};
</script>

<style></style>
